﻿using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Common;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Lissan Williams (Echo conversion by David Mello)
////Date created: 07/08/2009
////Modified by: David Mello
////Date modified: 06/12/2013
////Reason: Echo conversion (\SuitePhoenix\SuiteGuiTests\SuiteW2PrintApproval\W2PrintApprovalShipAddress\content.txt)
//// and ULTI-104289 (https://ultidev/browse/ULTI-104289)
////*************************************************************************************************************************************

namespace Echo.Automation.Payroll.W2_Print_Approval
{
    using System;

    using Echo.Models.UltimateSoftware.WebUITest.MbUnit;

    [TestFixture]
    [Author("David Mello", "david_mello@ultimatesoftware.com")]
    [Category("W2 Print Approval")]
    [Release("10.7.2")]
    [Header("Config")]
    [Config(".NET")]
    public class W2PrintApprovalShipAddress : MbUnitAutomatedTest
    {
        UltiPro ultiPro;
        UltiProDatabaseContext database;

        [FixtureSetUp]
        public void FixtureSetUp()
        {
            this.ultiPro = Launch<UltiPro>();
            this.database = Init<UltiProDatabaseContext>();
            this.database.Company = ConfigDatabase("ULTIPRO_SB340");
            this.database.Company.AddRollBack("paw2ApprovalDates");
            this.database.Company.AddRollBack("paw2Approvals");

            // Insert record into paw2ApprovalDates to enable print approval.
            this.database.Company.Execute("DELETE FROM paw2ApprovalDates");
            this.DeleteShippingPreference("WP5");
            this.database.Company.Execute(string.Format(
                "INSERT INTO paw2ApprovalDates (paaFinalApprovalDate, paaYear) VALUES ('{0}',2008)",
                DateTime.Now.AddDays(1).ToShortDateString()));

            // Login and click Reporting > W2 Print Approval.
            this.ultiPro.Pages.Login.LoginAs("GreenT", "Password1", Config.LanguageCode);

            this.ultiPro.Menu.Reporting.Click(link => link.W2PrintApproval);
            this.ultiPro.Pages.W2PrintApprovalSummary.W2PrintApprovalSummaryGrid.Row(row => row.TaxGroupLink, "WP5").Click(link => link.TaxGroupLink);
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            DeleteShippingPreference("WP5");
            this.database.Company.RollBackDatabase();
            this.ultiPro.Browser.Quit();
        }

        [SetUp]
        public void TestSetup()
        {
            FillInEmailAndCopyDRecipient();
        }

        [TearDown]
        public void TearDown()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.ButtonBar.Click(buttons => buttons.Reset);
            this.ultiPro.Pages.W2PrintApprovalDetail.ErrorMessages.NotExists(errors => errors.MessageBox);
        }

        /// <summary>
        /// ULTI-104289 - When a post office box is entered in the company shipping address field, the address should not be saved.
        /// Post office box addresses are not permitted for W-2 shipping.  This test ensures PO Box address variants on line 1 or 2 of the
        /// shipping information is not permitted.  The user should receive an error, "Address may not include PO Box", blocking the save at the top of the page.
        /// </summary>
        /// <param name="line1">Value for Address Line 1.</param>
        /// <param name="line2">Value for Address Line 2.</param>
        [Test]
        [Row("PO BOX 220", "Suite 3")]
        [Row("1 Merry Way", "PO. BOX 220")]
        [Row("P.O. BOX 220", "Apt 1")]
        [Row("3/4 Way", "P.O BOX 220")]
        [Row("p.o. box 123", "")]
        [Row("Dubya Two", "po box 123")]
        [Row("P O Box 9999", "")]
        [Row("1 Tax Shelter Dr", "P  o  Box 222 2")]
        [Row("po. Box G", "Ste 3")]
        [Row("Post office box", "9990")]
        [Row("P.O. Box", "987")]
        [Row("1 Random Way", "PO Box")]
        [Row("Post", "Office Box 3")]
        public void PoBoxAddressesShouldNotBeAllowed(string line1, string line2)
        {
            // Given I've entered a post office box address in one of the shipping address fields.
            this.ultiPro.Pages.W2PrintApprovalDetail.Set(form => form.AddressLine1TextBox, line1);
            this.ultiPro.Pages.W2PrintApprovalDetail.Set(form => form.AddressLine2TextBox, line2);

            // When I click Save.
            this.ultiPro.Pages.W2PrintApprovalDetail.ButtonBar.Click(b => b.Save);

            // Then I should receive an error.
            this.ultiPro.Pages.W2PrintApprovalDetail.ErrorMessages.Like(errors => errors.MessageBox, "innerHtml", "Address may not include PO Box");
        }

        private void DeleteShippingPreference(string taxGroup)
        {
            this.database.Company.Execute(string.Format("DELETE FROM paw2approvals WHERE pawTaxGroup = '{0}'", taxGroup));
        }

        /// <summary>
        /// Fills in required fields email and copy D recipient.
        /// </summary>
        private void FillInEmailAndCopyDRecipient()
        {
            this.ultiPro.Pages.W2PrintApprovalDetail.Set(
                form => form.PrimaryEmailAddressTextBox, "elizsmith@domain.com");
            this.ultiPro.Pages.W2PrintApprovalDetail.Set(x => x.CopyRecipientsDropDown, "BLACKM", SelectOption.Value);
        }
    }
}
